package com.example.goods_manage.service.impl;

import com.example.goods_manage.dto.Goods;
import com.example.goods_manage.dto.request.GoodsRequest;
import com.example.goods_manage.mapper.GoodsMapper;
import com.example.goods_manage.service.GoodsService;
import com.example.goods_manage.utils.AjaxResult;
import com.example.goods_manage.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
@SuppressWarnings({"unchecked", ""})
public class GoodsServiceImpl implements GoodsService {
    private static final Logger log = LoggerFactory.getLogger(GoodsServiceImpl.class);

    @Resource
    private GoodsMapper goodsMapper;


    @Override
    public AjaxResult<PageUtils> findGoods(GoodsRequest goodsRequest) {
        PageHelper.startPage(goodsRequest.getPageNum(), goodsRequest.getPageSize());
        List<Goods> goodsList = goodsMapper.findGoods(goodsRequest);
        PageInfo<Goods> pageAll = PageInfo.of(goodsList);
        PageUtils pageUtils = new PageUtils(pageAll);
        return AjaxResult.success(pageUtils);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult<Void> deleteGoods(GoodsRequest goodsRequest) {
        int i = goodsMapper.deleteByPrimaryKey(goodsRequest.getId());
        if (i > 0) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败,可能不存在此条记录");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult<Void> updateGoods(GoodsRequest goodsRequest) {
        if (ObjectUtils.isEmpty(goodsRequest.getId())) {
            throw new NullPointerException("更新操作 主键id不能为空");
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest, goods);
        int i = goodsMapper.updateByPrimaryKeySelective(goods);
        if (i > 0) {
            return AjaxResult.success("更新成功");
        } else {
            return AjaxResult.error("更新失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult<Void> addGoods(GoodsRequest goodsRequest) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest, goods);
        //checkName();
        int i = goodsMapper.insertSelective(goods);
        if (i > 0) {
            return AjaxResult.success("新增成功");
        } else {
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 获取配置的值 如果没有，则默认为20
    */
    @Value("${goods.retrieval-goods-name-result:20}")
    private Integer retrievalGoodsNameResult;
    @Override
    public List<Map> retrievalGoodsName(String goodsName) {
        //进行分页，如果名称很多，返回这么多也没有意义，只要返回前面匹配度比较高的即可
        PageHelper.startPage(1,retrievalGoodsNameResult);
        List<Map> goodsList= goodsMapper.retrievalGoodsByName(goodsName);
        return goodsList;
    }


}
